/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import pojo.KhachHang;
import util.HibernateUtil;

/**
 *
 * @author LaiHuynh
 */
public class KhachHangDAO {

    public static List<KhachHang> layDanhSachKhachHang() {
        List<KhachHang> ds = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            String hql = "select kh from KhachHang kh";
            org.hibernate.Query query = session.createQuery(hql);
            ds = query.list();
        } catch (HibernateException ex) {
            //Log the exception 
            System.err.println(ex);
        } finally {
            session.close();
        }
        return ds;
    }

    public static List<KhachHang> timKiemKhachHang(String hoTenKhachHang, int maCuaHang) {
        List<KhachHang> ds = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            //String hql = "select sp from SanPham sp where sp.tenSanPham like :tenSanPham and sp.hangSanXuat.maHangsx =:maHangsx";
            String hql = "select kh from KhachHang kh where kh.hoTenKhachHang like '%"
                    + hoTenKhachHang
                    + "%' and kh.cuaHang.maCuaHang ="
                    + maCuaHang;
            org.hibernate.Query query = session.createQuery(hql);
            //query.setString("tenSanPham", "%"+tenSanPham+"%");
            //query.setInteger("maHangsx", maHangsx);
            ds = query.list();
        } catch (HibernateException ex) {
            //Log the exception 
            System.err.println(ex);
        } finally {
            session.close();
        }
        return ds;
    }

    //code-phi
    public static KhachHang layThongTinKhachHang(int maKhachHang) {
        KhachHang kh = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            kh = (KhachHang) session.get(KhachHang.class, maKhachHang);
            // Hibernate.initialize(qd.getCuaHang());
        } catch (HibernateException ex) {
            //Log the exception
            System.err.println(ex);
        } finally {
            session.close();
        }
        return kh;
    }

    public static boolean themThongTinKhachHang(KhachHang kh) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        if (KhachHangDAO.layThongTinKhachHang(kh.getMaKhachHang()) != null) {
            return false;
        }
        boolean kq = true;
        Transaction transaction = null;
        try {
            transaction = session.beginTransaction();
            session.save(kh);
            transaction.commit();
        } catch (HibernateException ex) {
            //Log the exception
            transaction.rollback();
            System.err.println(ex);
            kq = false;
        } finally {
            session.close();
        }
        return kq;
    }

    public static boolean xoaKhachHang(int maKhachHang) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        KhachHang kh = KhachHangDAO.layThongTinKhachHang(maKhachHang);
        if (kh == null) {
            return false;
        }
        boolean kq = true;
        Transaction transaction = null;
        try {
            transaction = session.beginTransaction();
            session.delete(kh);
            transaction.commit();
        } catch (HibernateException ex) {
            //Log the exception
            transaction.rollback();
            System.err.println(ex);
            kq = false;
        } finally {
            session.close();
        }
        return kq;
    }

    public static List<KhachHang> timKhachHang(String hoTenKhachHang, int maKhachHang) {
        List<KhachHang> ds;
        ds = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            String hql = "select kh from KhachHang kh where kh.hoTenKhachHang like '%"
                    + hoTenKhachHang + "%' and kh.cuaHang.maCuaHang="
                    + maKhachHang;
            Query query = session.createQuery(hql);
            ds = query.list();
        } catch (HibernateException ex) {
            System.err.println(ex);
        } finally {
            session.close();
        }
        return ds;
    }
    public static KhachHang layTienNoKhachHang(int maCuaHang) {
        KhachHang kh = null; 
        Session session = HibernateUtil.getSessionFactory().openSession();
        try { 
            String hql = "select kh from KhachHang kh where kh.CuaHang.maCuaHang =:maCuaHang";
            org.hibernate.Query query = session.createQuery(hql); 
            query.setInteger("maCuaHang", maCuaHang);
            kh = (KhachHang)query.uniqueResult(); 
        } catch (HibernateException ex) { 
            //Log the exception 
            System.err.println(ex); 
        } finally { 
            session.close(); 
        } 
        return kh;
    }
    public static void capNhatThongTinKhachHang(KhachHang kh) { 
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        session.update(kh);
        session.getTransaction().commit();
        session.close(); 
    }
}
